package com.mohe.dangjian.timmer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.mohe.dangjian.model.Member;
import com.mohe.dangjian.model.Remind;
import com.mohe.dangjian.service.interfaces.IMember;
import com.mohe.dangjian.service.interfaces.IRemind;
import com.mohe.dangjian.utils.DateStrUtil;

/**
 * 定时器
 * @author wxm
 *
 */
public class Timmer {
	private IMember memberServ;
	private IRemind remindServ;
	
	public IMember getMemberServ() {
		return memberServ;
	}
	public void setMemberServ(IMember memberServ) {
		this.memberServ = memberServ;
	}
	public IRemind getRemindServ() {
		return remindServ;
	}
	public void setRemindServ(IRemind remindServ) {
		this.remindServ = remindServ;
	}
	
	/**
	 * 检查是否有预备党员需要转正，如果有则添加预备党员转正提醒
	 */
	public void checkMemberConfirm(){
		// 获取预备党员
		Member member = new Member();
		member.setMemberType("预备党员");
		List<Member> list = memberServ.getMemberList(member, new HashMap<String,String>());
		// 找出需要转正的预备党员
		List<Member> clist = new ArrayList<Member>();
		if(list != null && list.size() > 0){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(Member m : list){
				try {
					Date d1 = new Date();
					Date d2 = sdf.parse(m.getJoinDate());
					boolean flag = DateStrUtil.isDiffOneYear(d1, d2);
					if(flag){
						clist.add(m);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			// 添加提醒	
			List<Remind> rlist = new ArrayList<Remind>();
			for(Member m : clist){
				Remind remind = new Remind();
				String name = m.getName() + "预备党员转正";
				String content = m.getName() + "预备党员转正";
				String happenDate = sdf.format(new Date());
				String remindDate = sdf.format(new Date());
				remind.setName(name);
				remind.setContent(content);
				remind.setHappenDate(happenDate);
				remind.setRemindDate(remindDate);
				rlist.add(remind);
			}
			remindServ.saveRemindList(rlist);
		}
	}
}
